using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Xml;

using OR.Reuse.Persistence;

using OR.Lazarus.Config;
using OR.Lazarus.Business.Entities.Log;

namespace OR.Lazarus.Windows.UserControls
{
	/// <summary>
	/// Summary description for UCCompilerFiles.
	/// </summary>
	public class UCCompilerFiles : System.Windows.Forms.UserControl
	{
		private	const	string	TITLE_LAZARUS	= "Lazarus 3.0";
		private	const	string	FILTER_XML		= "XML Files (*.xml)|*.xml";
		private	const	string	DEFAULT_EXT		= "xml";

		private	const	string	TITLE_OPENCONFIG	= "Choose an XML Configuration file to use:";
		private	const	string	TITLE_OPENSOURCE	= "Choose an XML Source file to compile:";

		private System.Windows.Forms.TextBox	m_txtConfig;
		private System.Windows.Forms.Label		m_lblConfig;
		private System.Windows.Forms.Button		m_btnConfig;
		private System.Windows.Forms.Button		m_btnSource;
		private System.Windows.Forms.Label		m_lblSource;
		private System.Windows.Forms.TextBox	m_txtSource;

		private LogDetail								m_oLog;

		public LogDetail Log
		{
			get
			{
				return ( m_oLog );
			}
			set
			{
				m_oLog = value;
			}
		}

		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public UCCompilerFiles()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			m_oLog = new LogDetail();
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if ( disposing )
			{
				if ( components != null )
				{
					components.Dispose();
				}
			}

			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.m_txtConfig = new System.Windows.Forms.TextBox();
			this.m_lblConfig = new System.Windows.Forms.Label();
			this.m_btnConfig = new System.Windows.Forms.Button();
			this.m_btnSource = new System.Windows.Forms.Button();
			this.m_lblSource = new System.Windows.Forms.Label();
			this.m_txtSource = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// m_txtConfig
			// 
			this.m_txtConfig.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.m_txtConfig.Location = new System.Drawing.Point(112, 8);
			this.m_txtConfig.Name = "m_txtConfig";
			this.m_txtConfig.Size = new System.Drawing.Size(400, 20);
			this.m_txtConfig.TabIndex = 0;
			this.m_txtConfig.Text = "";
			// 
			// m_lblConfig
			// 
			this.m_lblConfig.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.m_lblConfig.Location = new System.Drawing.Point(8, 8);
			this.m_lblConfig.Name = "m_lblConfig";
			this.m_lblConfig.TabIndex = 1;
			this.m_lblConfig.Text = "Config File:";
			// 
			// m_btnConfig
			// 
			this.m_btnConfig.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.m_btnConfig.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.m_btnConfig.Location = new System.Drawing.Point(512, 8);
			this.m_btnConfig.Name = "m_btnConfig";
			this.m_btnConfig.Size = new System.Drawing.Size(24, 23);
			this.m_btnConfig.TabIndex = 2;
			this.m_btnConfig.Text = "...";
			this.m_btnConfig.Click += new System.EventHandler(this.DoOpenConfig);
			// 
			// m_btnSource
			// 
			this.m_btnSource.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.m_btnSource.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.m_btnSource.Location = new System.Drawing.Point(512, 40);
			this.m_btnSource.Name = "m_btnSource";
			this.m_btnSource.Size = new System.Drawing.Size(24, 23);
			this.m_btnSource.TabIndex = 5;
			this.m_btnSource.Text = "...";
			this.m_btnSource.Click += new System.EventHandler(this.DoOpenSource);
			// 
			// m_lblSource
			// 
			this.m_lblSource.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.m_lblSource.Location = new System.Drawing.Point(8, 40);
			this.m_lblSource.Name = "m_lblSource";
			this.m_lblSource.TabIndex = 4;
			this.m_lblSource.Text = "Source File:";
			// 
			// m_txtSource
			// 
			this.m_txtSource.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.m_txtSource.Location = new System.Drawing.Point(112, 40);
			this.m_txtSource.Name = "m_txtSource";
			this.m_txtSource.Size = new System.Drawing.Size(400, 20);
			this.m_txtSource.TabIndex = 3;
			this.m_txtSource.Text = "";
			// 
			// UCCompilerFiles
			// 
			this.Controls.Add(this.m_btnSource);
			this.Controls.Add(this.m_lblSource);
			this.Controls.Add(this.m_txtSource);
			this.Controls.Add(this.m_btnConfig);
			this.Controls.Add(this.m_lblConfig);
			this.Controls.Add(this.m_txtConfig);
			this.Name = "UCCompilerFiles";
			this.Size = new System.Drawing.Size(544, 64);
			this.ResumeLayout(false);

		}
		#endregion

		public string ConfigFile
		{
			get
			{
				return ( m_txtConfig.Text );
			}
			set
			{
				m_txtConfig.Text = value;
			}
		}

		public string SourceFile
		{
			get
			{
				return ( m_txtSource.Text );
			}
			set
			{
				m_txtSource.Text = value;
			}
		}

		public XmlDocument ConfigDocument
		{
			get
			{
				return ( this.GetConfig() );
			}
		}

		public XmlDocument SourceDocument
		{
			get
			{
				return ( this.GetSource() );
			}
		}

		public XmlDocument GetConfig()
		{
			if ( String.IsNullOrEmpty( m_txtConfig.Text ) )
			{
				throw new ApplicationException( "No Config File specified." );
			}

			XmlDocument						v_oDocument	= new XmlDocument();
			
			try
			{
				v_oDocument.Load( m_txtConfig.Text );
			}
			catch ( XmlException ex )
			{
				throw new ApplicationException( "Error in file [ file://" + m_txtConfig.Text + " ]: " + ex.Message );
			}

			return ( v_oDocument );
		}

		public XmlDocument GetSource()
		{
			if ( String.IsNullOrEmpty( m_txtSource.Text ) )
			{
				throw new ApplicationException( "No Source File specified." );
			}

			XmlDocument						v_oDocument	= new XmlDocument();
			
			try
			{
				v_oDocument.Load( m_txtSource.Text );
			}
			catch ( XmlException ex )
			{
				throw new ApplicationException( "Error in file [ file://" + m_txtSource.Text + " ]: " + ex.Message );
			}


			return ( v_oDocument );
		}

		private void DoOpenConfig(object sender, System.EventArgs e)
		{
			OpenFileDialog	v_oFile	= new OpenFileDialog();
			
			v_oFile.Title					= TITLE_OPENCONFIG;
			v_oFile.DefaultExt			= DEFAULT_EXT;
			v_oFile.Filter					= FILTER_XML;
			v_oFile.InitialDirectory	= LazarusConfig.ConfigPath;

			if ( v_oFile.ShowDialog() == DialogResult.OK )
			{
				m_txtConfig.Text = v_oFile.FileName;
			}
		}

		private void DoOpenSource(object sender, System.EventArgs e)
		{
			OpenFileDialog v_oFile	= new OpenFileDialog();

			v_oFile.Title					= TITLE_OPENSOURCE;
			v_oFile.DefaultExt			= DEFAULT_EXT;
			v_oFile.Filter					= FILTER_XML;
			v_oFile.InitialDirectory	= LazarusConfig.SourcePath;

			if ( v_oFile.ShowDialog() == DialogResult.OK )
			{
				m_txtSource.Text = v_oFile.FileName;
			}
		}
	}
}
